﻿Imports Cls_Common
Imports VSModel
Partial Class Control_Feedback_Create
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack AndAlso Not Session("UserId") Is Nothing Then
            Dim _UserId As Integer = Session("UserId")
            Using _data As New VSEntities
                Dim p = (From q In _data.Users Where q.UserId = _UserId Select q).FirstOrDefault

                If Not p Is Nothing Then
                    txtCustomerName.Text = IIf(Not p.FullName Is Nothing, p.FullName, "")
                    txtCustomerEmail.Text = IIf(Not p.Email Is Nothing, p.Email, "")
                End If

            End Using

        End If
        If Session("LanguageId") = 2 Then
            hplTitle.Text = "Feedback"
            ltlFeedbackLabel.Text = "Please contribute your valuable comments by entering the sample form below:"
            ltlRequiredToEnterLabel.Text = "Required to enter"
            lblCustomerName_edit.Text = "Your Name:"
            lblCustomerAddress_edit.Text = "Address:"
            lblCustomerPhone_edit.Text = "Phone number:"
            lblCustomerNote_edit.Text = "Content:"
            btnSave.Text = "Send"
        End If
    End Sub
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using _data As New VSEntities
            If (Session("PostedFeedback")) Is Nothing Or _
                    (DateDiff(DateInterval.Second, CType(Session("PostedFeedback"), Date), Now()) >= ConfigurationManager.AppSettings("SettingTimeUpdatePageView")) Then
                Try
                    Dim p As New VSModel.Feedback
                    p.FullName = txtCustomerName.Text
                    p.Email = txtCustomerEmail.Text
                    p.Address = txtCustomerAddress.Text
                    p.Phone = txtCustomerPhone.Text
                    p.Content = txtQuestion.Text
                    p.Created = Now
                    p.StatusId = 2
                    _data.Feedbacks.AddObject(p)
                    _data.SaveChanges()
                    If Session("LanguageId") = 1 Then
                        Excute_Javascript("Alertbox('Ý kiến của bạn đã được gửi. Cám ơn bạn đã đóng góp!');", Me.Page)
                    Else
                        Excute_Javascript("Alertbox('Your comment has been submitted. Thanks for your contribution!');", Me.Page)
                    End If
                    Session("PostedFeedback") = Now
                Catch ex As Exception
                    If Session("LanguageId") = 1 Then
                        Excute_Javascript("Alertbox('Lưu thất bại hãy kiểm tra lại');", Me.Page)
                    Else
                        Excute_Javascript("Alertbox('Save failed check');", Me.Page)
                    End If
                End Try
            Else
                If Session("LanguageId") = 1 Then
                    Excute_Javascript("Alertbox('Ý kiến của bạn trước đó đang được xử lý. Vậy bạn vui lòng đợi ít phút trước khi gửi ý kiến tiếp theo. Cảm ơn bạn!');", Me.Page)
                Else
                    Excute_Javascript("Alertbox('Your comments before being processed. So please wait a few minutes before next post comments. Thank you!');", Me.Page)
                End If
            End If
        End Using
    End Sub
End Class
